// supplyChain/pages/apply/apply.js
var common = require("../../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        hasApply: 0,
        imgbox: [],
        uniacidList: [],
        cateList: [],
        imgbox: [],
        uniacid: "",
        title: "",
        contacts_man: "",
        contacts_phone: "",
        information: "",
        classify_id: "",
        province_code: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        this.getBase();
        this.getCategory();
        this.getProvince();
        this.getUniacid();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    getUniacid: function() {
        var that = this;
        common.request({
            endpoint: "Supply/supply/uniacid-all",
            method: "post",
            data: {
                uniacid: 115
            },
            success: res => {
                var uniacidList = res.extra;
                var hasApply = that.data.hasApply;
                uniacidList.filter(item => {
                    if (item.status != 2) {
                        hasApply = 1;
                    }
                    return item.status == 2;
                });
                that.setData({
                    hasApply: hasApply,
                    uniacidList: res.extra
                });
            }
        });
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor
                });
            }
        });
    },
    saveInput: function(e) {
        let that = this;
        let key = e.currentTarget.dataset.name;
        that.setData({
            [key]: e.detail.value
        });
    },
    chooseCate: function(e) {
        let that = this;
        if (that.data.cateList.length) {
            that.setData({
                classify_id: that.data.cateList[e.detail.value].id,
                cate_name: that.data.cateList[e.detail.value].name
            });
        }
    },
    chooseProvince: function(e) {
        let that = this;
        that.setData({
            province_code: that.data.provinceList[e.detail.value].code,
            province_name: that.data.provinceList[e.detail.value].name
        });
    },
    apply: function(e) {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        if (!that.data.uniacid) {
            wx.showModal({
                title: "提示",
                content: "请选择站点"
            });
            return false;
        }
        if (!that.data.title) {
            wx.showModal({
                title: "提示",
                content: "供应链名称不能为空"
            });
            return false;
        }
        if (!that.data.contacts_phone) {
            wx.showModal({
                title: "提示",
                content: "手机号码不能为空"
            });
            return false;
        }
        var reg2 = /^[1][0-9][0-9]{9}$/;
        if (!reg2.test(that.data.contacts_phone)) {
            wx.showModal({
                title: "提示",
                content: "请填写正确手机号"
            });
            return false;
        }
        if (!that.data.contacts_man) {
            wx.showModal({
                title: "提示",
                content: "联系人不能为空"
            });
            return false;
        }
        // if (!that.data.classify_id) {
        //   wx.showModal({
        //     title: '提示',
        //     content: '请选择商家分类',
        //   })
        //   return false;
        // }
                if (!that.data.province_code) {
            wx.showModal({
                title: "提示",
                content: "请选择区域"
            });
            return false;
        }
        common.request({
            endpoint: "Supply/supply/supply",
            method: "post",
            data: {
                uniacid: that.data.uniacid,
                title: that.data.title,
                contacts_phone: that.data.contacts_phone,
                contacts_man: that.data.contacts_man,
                information: that.data.information,
                classify_id: that.data.classify_id,
                province_code: that.data.province_code,
                img: that.data.imgbox
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                wx.showToast({
                    title: "提交成功",
                    icon: "success",
                    success: res => {
                        setTimeout(() => {
                            wx.switchTab({
                                url: "/pages/personal/personal"
                            });
                        }, 500);
                    }
                });
            },
            fail: res => {
                wx.showModal({
                    title: "提示",
                    content: "申请失败," + res.message,
                    success: res => {
                        setTimeout(() => {
                            wx.switchTab({
                                url: "/pages/personal/personal"
                            });
                        }, 500);
                    }
                });
            }
        });
    },
    chooseUniacid: function(e) {
        let that = this;
        if (that.data.uniacidList.length) {
            that.setData({
                uniacid: that.data.uniacidList[e.detail.value].uniacid,
                uniacid_name: that.data.uniacidList[e.detail.value].uniacid_name
            });
        }
    },
    //上传图片
    uploadImage: function() {
        var that = this;
        let imgbox = that.data.imgbox;
        let domian = app.globalData.domain;
        let len = that.data.imgbox.length;
        let count = 6 - len;
        let uniacid;
        if (wx.getStorageSync("uniacid")) {
            uniacid = wx.getStorageSync("uniacid");
        } else {
            uniacid = un.uniacid;
        }
        wx.chooseImage({
            count: count,
            //最多可以选择的图片总数
            sizeType: [ "compressed" ],
            // 可以指定是原图还是压缩图，默认二者都有
            sourceType: [ "album", "camera" ],
            // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths;
                //启动上传等待中...
                                wx.showLoading({
                    title: "正在上传..."
                });
                var i = 0;
                var length = tempFilePaths.length;
                var upload = function() {
                    wx.uploadFile({
                        url: domian + "Supply/supply/upload-img",
                        filePath: tempFilePaths[i],
                        name: "file",
                        formData: {},
                        header: {
                            "Content-Type": "multipart/form-data",
                            uniacid: uniacid
                        },
                        success: function(res) {
                            var data = JSON.parse(res.data);
                            imgbox.push(data.extra);
                            i++;
                            if (i < length) {
                                upload();
                            } else {
                                wx.hideLoading();
                                that.setData({
                                    imgbox: imgbox
                                });
                            }
                        },
                        fail: function(res) {
                            wx.hideToast();
                            wx.showModal({
                                title: "错误提示",
                                content: "上传图片失败",
                                showCancel: false,
                                success: function(res) {}
                            });
                        }
                    });
                };
                upload();
            }
        });
    },
    //删除图片
    delete_img: function(e) {
        let that = this;
        let index = e.currentTarget.dataset.index;
        let imgbox = that.data.imgbox;
        imgbox.splice(index, 1);
        that.setData({
            imgbox: imgbox
        });
    },
    getCategory: function() {
        var that = this;
        common.request({
            endpoint: "Supply/supply/category",
            method: "post",
            data: {},
            success: res => {
                that.setData({
                    cateList: res.extra
                });
            }
        });
    },
    //获取省份
    getProvince: function() {
        var that = this;
        common.request({
            endpoint: "Lodger/lodger/get-province",
            method: "post",
            data: {},
            success: res => {
                that.setData({
                    provinceList: res.extra
                });
            }
        });
    },
    //已申请的站点
    onPageTap: function(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    }
});